﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsPlanBusiness
    {
        public static bool DelPlan(string strCode)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    ClsCommDA.Del("BS_PlanDetail", "PlanID", strCode, broker);
                    ClsCommDA.Del("BS_Plan", "PlanID", strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
            }
            return istag;
        }

        public static DataTable SelectList(string PlanID, string From, string End, int pageIndex, int PageSize, out int m_count)
        {
            return BsPlanDataAccess.SelectList(PlanID, From, End, pageIndex, PageSize, out m_count);
        }

        public static bool SaveOrUpdate(BsPlanEntity entity, DataTable PlanDetailDT, string type)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (type == "Save")
                    {
                        DataAccessCommon.Insert(entity, broker);
                    }
                    else if (type == "Update")
                    {
                        DataAccessCommon.UpdateOnly(entity, broker);
                    }
                    if (PlanDetailDT != null && PlanDetailDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in PlanDetailDT.Rows)
                        {
                            BsPlandetailEntity en = new BsPlandetailEntity();
                            en.PlanID = entity.PlanID;
                            en.BOMDetailID = CommonMethod.ConvertToDecimal(drItem["BOMDetailID"]);
                            en.MaterialID = CommonMethod.GetString(drItem["MaterialID"]);
                            en.TotalNum = CommonMethod.ConvertToDecimal(drItem["TotalNum"]);
                            en.TotalWeight = CommonMethod.ConvertToDecimal(drItem["TotalWeight"]);

                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                               DataAccessCommon.Insert(en, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                en.PlanDetailID = CommonMethod.ConvertToInt(drItem["PlanDetailID"]);
                                DataAccessCommon.UpdateOnly(en, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_PlanDetail", "PlanDetailID", CommonMethod.GetString(drItem["PlanDetailID"]), broker);
                            }
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
                return istag;
            }
        }

        public static DataTable SelectPlanByWorkOrderNo(string workorderno)
        {
            return BsPlanDataAccess.SelectPlanByWorkOrderNo(workorderno);
        }

        public static DataTable SelectDialogList(string ProductID, string ProductName, string userid, int pageIndex, int PageSize, out int m_count)
        {
            return BsPlanDataAccess.SelectDialogList(ProductID, ProductName, userid, pageIndex, PageSize, out m_count);
        }

        public static bool IsExistsPlanID(string PlanID)
        {
            bool isResult = false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    string str = @"select * from BS_Plan where PlanID=@PlanID";
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    dpc.AddWithValue("@PlanID", PlanID);
                    DataTable d = DataAccessCommon.Select(str, dpc).Tables[0];
                    if (d != null && d.Rows.Count > 0)
                    {
                        isResult = true;
                    }
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = true;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static DataTable GetDataByPlanID(string id)
        {
            return BsPlanDataAccess.GetDataByPlanID(id);
        }
    }
}
